import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from A3mall_unittest.unittest_a3mall_db import Mydb
from ddt import ddt, data, unpack, file_data

class A3mall3(unittest.TestCase):

    def setUp(self) -> None:
        # 连接数据库
        self.db = Mydb('10.59.9.14', 'root', 'yidong666', 'a3mall', '3306')
        self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.get('http://10.59.9.14:7003/')
        # 隐式等待
        self.driver.implicitly_wait(10)

    def tearDown(self) -> None:
        # 关闭数据库连接
        self.db.close()
        time.sleep(5)
        # 退出浏览器
        self.driver.quit()

    # 用户总金额
    userprice = None

    def test_a3_01(self):
        # 1、点击登录
        self.driver.find_element(By.XPATH, '//a[@href="/home/login.html"]').click()
        # 断言1：会员登录
        dy1 = self.driver.find_element(By.CLASS_NAME, 'reg-new').text
        print(f'dy1:{dy1}')
        self.assertEqual('会员登录', dy1)
        # 2、输入用户名
        self.driver.find_element(By.NAME, 'username').send_keys('15985645484')
        # 3、输入密码
        self.driver.find_element(By.NAME, 'password').send_keys('123456')
        # 4、勾选记住密码
        self.driver.find_element(By.CSS_SELECTOR, '#theForm > div.agree-clause > span > label > input').click()
        # 强制等待10秒，输入验证码
        time.sleep(10)
        # 5、点击登录按钮
        self.driver.find_element(By.CLASS_NAME, 'finish-btn').click()
        time.sleep(5)
        # 断言2：欢迎回来，小高4
        dy2 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[1]').text
        print(f'dy2:{dy2}')
        self.assertIn('欢迎回来', dy2)
        price = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        price1 = str(price).split('¥ ')
        print(f"用户总金额:{price1[1]}")
        A3mall3.userprice = float(price1[1])

    def test_a3_02(self):
        try:
            # 前置条件 ：账号已登录
            self.test_a3_01()
            time.sleep(1)
            # 点击地址管理
            self.driver.find_element(By.XPATH, '/html/body/div[5]/div[1]/div[2]/div[4]/div[2]/ul/li[1]/a').click()
            time.sleep(1)
            # 浏览器返回事件
            self.driver.back()
            time.sleep(2)
            # 浏览器前进事件
            self.driver.forward()
            time.sleep(2)
            # 浏览器刷新事件
            self.driver.refresh()
            time.sleep(2)
            # 隐式等待
            self.driver.implicitly_wait(10)
            # 断言：地址管理
            dz = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[1]/span').text
            self.assertEqual('地址管理', dz)
            # 点击添加收货地址
            self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[1]/a').click()
            # 隐式等待
            self.driver.implicitly_wait(10)
            # 断言：编辑地址：//*[@id="layui-layer1"]/div[1]
            bjdz = self.driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[1]').text
            self.assertEqual('编辑地址', bjdz)
            # 选择所在地址
            # 隐式等待
            self.driver.implicitly_wait(10)
            self.driver.find_element(By.CSS_SELECTOR, '#layui-layer1 > div.layui-layer-content > div > div > form > div:nth-child(1) > div:nth-child(2) > div > div > input').click()
            self.driver.find_element(By.CSS_SELECTOR, '#layui-layer1 > div.layui-layer-content > div > div > form > div:nth-child(1) > div:nth-child(2) > div > dl > dd:nth-child(2)').click()
            # 隐式等待
            self.driver.implicitly_wait(10)
            self.driver.find_element(By.CSS_SELECTOR, '#layui-layer1 > div.layui-layer-content > div > div > form > div:nth-child(1) > div:nth-child(3) > div > div > input').click()
            # 隐式等待
            self.driver.implicitly_wait(10)
            self.driver.find_element(By.CSS_SELECTOR, '#layui-layer1 > div.layui-layer-content > div > div > form > div:nth-child(1) > div:nth-child(3) > div > dl > dd:nth-child(2)').click()
            # 隐式等待
            self.driver.implicitly_wait(10)
            self.driver.find_element(By.CSS_SELECTOR, '#layui-layer1 > div.layui-layer-content > div > div > form > div:nth-child(1) > div:nth-child(4) > div > div > input').click()
            # 隐式等待
            self.driver.implicitly_wait(10)
            self.driver.find_element(By.CSS_SELECTOR, '#layui-layer1 > div.layui-layer-content > div > div > form > div:nth-child(1) > div:nth-child(4) > div > dl > dd:nth-child(2)').click()
            # 隐式等待
            self.driver.implicitly_wait(10)
            # 输入收货人
            self.driver.find_element(By.NAME, 'accept_name').send_keys('小高先生')
            # 输入手机号:mobile
            self.driver.find_element(By.NAME, 'mobile').send_keys('15985645489')
            # 输入详细地址://*[@id="layui-layer1"]/div[2]/div/div/form/div[5]/div/textarea
            self.driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[2]/div/div/form/div[5]/div/textarea').send_keys('南口南大街')
            # 点击立即提交://*[@id="layui-layer1"]/div[2]/div/div/form/div[6]/div/button[1]
            self.driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[2]/div/div/form/div[6]/div/button[1]').click()
            # 数据库断言
            address = self.db.seatch(f"select address from mall_users_address where mobile = '{15985645489}'")[0][0]
            self.assertEqual('南口南大街', address)
        except Exception as e:
            print(f'异常信息：{e}')










if __name__ == '__main__':
    unittest.main()